# coding utf-8
import os, sys
home = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append('/home/lanwenhong/tianya/trunk/client/python')
from memlinkclient import *
import test_init
import urllib
import urllib2
import json

class IndexRemoveTest:
    def __init__(self, memlink_path, memlink_addr, wport, rport, dserver, dport, htdocs_path):
        self.bop = base.op.BaseOp(memlink_path, memlink_addr, wport, rport, dserver, dport, htdocs_path)


    def test(self. tablename, itemcount):
        self.bop.create_table(tablename, itemcount)
        ret, jstr = self.bop.create_test_data(tablename, itemcount)
        if ret == -1:
            return ret

        for i in xrange(1, itemcount):
            indexkey = 'item%d' % i
            ret = self.bop.create_index(tablename, indexkey, 'index_rebuild')
            if ret == -1:
                return ret
        
        jdic = json.loads(jstr)
        linelist = jdic['arrsy']
        keylist = []

        for i in xrange(1, itemcount):
            item = 'item%d' % i
            for line in linelist:
                value = line[item]
                mkey = '%s%s_%s' % ('index_rebuild', item, value)
                if mkey not in midic:
                    keylist.append(meky)

        m = MemLinkClient(self.bop.tinit.memlink_addr, self.bop.tinit.rport, self.bop.tinit.wport, 30)
        for key in keylist:
            ret, mstat = m.stat(key)
            if mstat.data_used == 0:
                return -1

        for i in range(1, itemcount):
            indexkey = 'item%d' % i
            ret = self.bop.remove_index(tablename, indexkey)
            if ret == -1:
                return ret
        

        for key in keylist:
            ret, mstat = m.stat(key)
            if mstat.data_used != 0:
                return -1

        m.destroy()
        return 0

def index_remove_test():
    iatest = IndexRemoveTest('/home/lanwenhong/tianya/trunk', '127.0.0.1', 16001, 16002, '127.0.0.1', 8800, '/home/lanwenhong/htdocs')
    ret = iatest.test('index_remove_test_table', 4)
    return ret
